package gui.menu;

import gui.DialogCollection;
import gui.GUI;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;

import control.SelectPlayersMenuManager;

import data.Player;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;

public class SelectMenuPlayerPane extends JPanel {
	private JCheckBox chckbxPlayerEnabled;
	private SelectPlayersMenuManager manager;
	private boolean btnPlKeyCodePressed = false;

	private final Player pl;

	private final SelectMenuPlayerPane me = this;
	/**
	 * Create the panel.
	 * @param manager 
	 */
	public SelectMenuPlayerPane(final Player pl, final SelectPlayersMenuManager manager) {
		this.manager = manager;
		this.pl = pl;
		setMinimumSize(new Dimension(175, 125));
		setOpaque(false);

		chckbxPlayerEnabled = new JCheckBox("");
		chckbxPlayerEnabled.setOpaque(false);

		chckbxPlayerEnabled.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource().equals(chckbxPlayerEnabled)){
					if(manager.playerRequestsStateChange(pl, chckbxPlayerEnabled.isSelected())){
						me.updatePlayerEnabledState();
					}
				}
			}
		});
		
		final JButton btnPlName = new JButton(pl.getName());
		btnPlName.setBackground(GUI.BGMENU);
		btnPlName.setForeground(GUI.FGMENU);
		btnPlName.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String newName = DialogCollection.showPlayerNameChangeDialog(pl);
				if(!manager.playerRequestsNameChange(pl, newName))DialogCollection.showMessageDialog("New name is not valid. The old name will be restored.");
				btnPlName.setText(pl.getName());
				
			}
		});
		
		final JButton btnPlKeyCode = new JButton("Buzzer: " + KeyEvent.getKeyText(pl.getBuzzerKeyCode()));
		btnPlKeyCode.setBackground(GUI.BGMENU);
		btnPlKeyCode.setForeground(GUI.FGMENU);
		btnPlKeyCode.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				btnPlKeyCodePressed = true;
				btnPlKeyCode.setText("Change buzzer key now.");
			}
			
			@Override
			public void mouseReleased(MouseEvent e) {
				btnPlKeyCodePressed = false;
				btnPlKeyCode.setText("Buzzer: " + KeyEvent.getKeyText(pl.getBuzzerKeyCode()));
			}
			
		});
		
		btnPlKeyCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(btnPlKeyCodePressed){
					if(!manager.playerRequestsBuzzKeyChange(pl, e.getKeyCode(), e.getModifiers())){
						DialogCollection.showMessageDialog("New buzzer key is not valid. The old buzzer will be restored.");
						
						btnPlKeyCode.revalidate();
					}
					
					btnPlKeyCode.setText("New Buzzer: " + KeyEvent.getKeyText(pl.getBuzzerKeyCode()));
					btnPlKeyCodePressed = false;
				}
			}
		});
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnPlKeyCode, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
						.addComponent(chckbxPlayerEnabled, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
						.addComponent(btnPlName, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(chckbxPlayerEnabled, GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnPlName, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnPlKeyCode, GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
					.addContainerGap())
		);
		setLayout(groupLayout);

		this.updatePlayerEnabledState();


	}

	private void updatePlayerEnabledState(){
		if(pl.isEnabled()){
			this.setBorder(new LineBorder(new Color(0, 255, 0), 2, true));
			chckbxPlayerEnabled.setSelected(true);
		}
		else{
			setBorder(new LineBorder(Color.LIGHT_GRAY, 2, true));
			chckbxPlayerEnabled.setSelected(false);
		}
	}
}
